//
//  MessageLineViewController.m
//  shopping
//
//  Created by 徐双 on 2017/8/30.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "MessageLineViewController.h"
#import "MessageLineViewCell.h"
#import "MessageDetailViewController.h"

@interface MessageLineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation MessageLineViewController

- (void)viewWillAppear:(BOOL)animated{
    
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self creationDate];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.title = @"通知详情";
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;
    
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageLineViewCell" bundle:nil] forCellReuseIdentifier:@"messageCell"];
    
    UIView *viewOf = [[UIView alloc] init];
    viewOf.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1);
    [self.view addSubview:viewOf];
    viewOf.backgroundColor = [UIColor colorWithRed:204 / 255.0f green:204 / 255.0f  blue:204 / 255.0f  alpha:1];
    
    self.tableView.frame = CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);
}
- (void)creationDate{
    _dataArr = [[NSMutableArray alloc] init];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
  
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
 //   NSString *token = @"32728a411b83e29b9836262a72477e3e0dac";
    
    [productParams setValue:token forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:adminMymessageGetmessage networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        if ([[returnData objectForKey:@"status"]intValue]==1) {
            
            _dataArr = [returnData objectForKey:@"data"];
            NSLog(@"!!!!!!!!!!!!!!!%@", _dataArr);
            
            [self.view addSubview:self.tableView];
            [self.tableView reloadData];
            
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;

}

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 81;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MessageLineViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"messageCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    
    cell.time.text = [dic objectForKey:@"createTime"];
    cell.contentLabel.text = [dic objectForKey:@"msgContent"];
    cell.titlelabel.text = [dic objectForKey:@"msgTitle"];
    
    if ([[dic objectForKey:@"msgType"] isEqualToString:@"0"]) {
        
        cell.img.image = [UIImage imageNamed:@"tongzhi_icon_huangse"];
        
    }else if ([[dic objectForKey:@"msgType"] isEqualToString:@"1"]){
    
        cell.img.image = [UIImage imageNamed:@"tongzhi_icon_fense"];
        
    }else{
        
        cell.img.image = [UIImage imageNamed:@"tongzhi_icon_lanse"];
    }
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MessageDetailViewController * detail = [[MessageDetailViewController alloc] init];
    
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    
    detail.msgType = [dic objectForKey:@"msgType"];
    
    if ([[dic objectForKey:@"msgType"] isEqualToString:@"0"]) {
        detail.typeTitle = @"0";
    }else if ([[dic objectForKey:@"msgType"] isEqualToString:@"1"]) {
        detail.typeTitle = @"1";
    }else {
    
        detail.typeTitle = @"2";
        
    }
    
    [self.navigationController pushViewController:detail animated:YES];
    


}

@end
